package com.syqc.maps.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.annotation.DrawableRes
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.BitmapDescriptor
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.LatLngBounds
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.MarkerOptions
import com.amap.api.maps.model.MyLocationStyle
import com.amap.api.maps.model.Poi
import com.amap.api.navi.AmapNaviPage
import com.amap.api.navi.AmapNaviParams
import com.amap.api.navi.AmapNaviType
import com.amap.api.navi.AmapPageType
import com.syqc.comlib.callback.INaviListener
import com.syqc.comlib.utils.AppContext
import com.syqc.comlib.utils.NaviUtil

object MapUtil {
    //MarkerOptions
    private val mMarkerOptions = MarkerOptions()

    //导航回调
    val mNaviInfoCallback = SimpleINaviInfoCallback()

    private const val LATLNG_LAT = 0.0018
    private const val LATLNG_LNG = 0.0001
    val latLng = LatLng(23.973850, 120.979685)

    /**
     * 添加Marker
     */
    fun addMarker(aMap: AMap, latLng: LatLng, @DrawableRes imgId: Int) {
        aMap.addMarker(
            mMarkerOptions.position(latLng).icon(BitmapDescriptorFactory.fromResource(imgId))
        )
    }

    /**
     * 添加Marker
     */
    fun addStopMarker(aMap: AMap, latLng: LatLng, @DrawableRes imgId: Int): Marker {
        return aMap.addMarker(
            mMarkerOptions.position(latLng).icon(BitmapDescriptorFactory.fromResource(imgId))
        )
    }

    /**
     * 添加Marker
     */
    fun addMarker(aMap: AMap, latLng: LatLng, bitmapDesc: BitmapDescriptor) {
        aMap.addMarker(
            mMarkerOptions.position(latLng).icon(bitmapDesc)
        )
    }

    /**
     * 添加Marker
     */
    fun addTagMarker(aMap: AMap, latLng: LatLng, bitmapDesc: BitmapDescriptor, any: Any): Marker {
        val addMarker = aMap.addMarker(
            mMarkerOptions.position(latLng).icon(bitmapDesc)
        )
        addMarker.`object` = any
        return addMarker
    }

    /**
     * 添加Marker
     */
    fun addMarker(aMap: AMap, latLng: LatLng, @DrawableRes imgId: Int, any: Any): Marker {
        val addMarker = aMap.addMarker(
            mMarkerOptions.position(latLng).icon(BitmapDescriptorFactory.fromResource(imgId))
        )
        addMarker.`object` = any
        return addMarker
    }

    /**
     * 添加并获得Marker
     */
    fun addGetMarker(aMap: AMap, latLng: LatLng, @DrawableRes imgId: Int): Marker {
        return aMap.addMarker(
            mMarkerOptions.position(latLng).icon(BitmapDescriptorFactory.fromResource(imgId))
                .anchor(0.5f, 0.5f)
        )
    }

    /**
     * 添加并获得Marker
     */
    fun addAndGetMarker(aMap: AMap, latLng: LatLng, @DrawableRes imgId: Int): Marker {
        return aMap.addMarker(
            mMarkerOptions.position(latLng).icon(BitmapDescriptorFactory.fromResource(imgId))
                .anchor(0.5f, 0.8f)
        )
    }


    /**
     * 添加并获得Marker
     */
    fun addAndGetMarker(
        aMap: AMap, latLng: LatLng, @DrawableRes imgId: Int, rotate: Float
    ): Marker {
        return aMap.addMarker(
            mMarkerOptions.position(latLng).icon(BitmapDescriptorFactory.fromResource(imgId))
                .anchor(0.5f, 0.5f)
                .rotateAngle(rotate).belowMaskLayer(true)
        )
    }

    /**
     * 添加并获得Marker
     */
    fun addAndGetMarker(
        aMap: AMap, latLng: LatLng, descriptor: BitmapDescriptor, rotate: Float
    ): Marker {
        return aMap.addMarker(
            mMarkerOptions.position(latLng).icon(descriptor).anchor(0.5f, 0.5f)
                .rotateAngle(rotate).belowMaskLayer(true)
        )
    }

    /**
     * 添加并获得Marker
     */
    fun addAndGetMarker(
        aMap: AMap, latLng: LatLng, descriptor: BitmapDescriptor
    ): Marker {
        return aMap.addMarker(
            mMarkerOptions.position(latLng).icon(descriptor).anchor(0.12f, 0.5f)
                .belowMaskLayer(true)
        )
    }

    /**
     * 设置地图显示的中心的LatLng
     */
    fun showMapLatLng(aMap: AMap, latLng: LatLng) {
        val zoom = aMap.cameraPosition.zoom
        val rLat = if (zoom > 18f) 0.00015 else LATLNG_LAT
        val rLng = if (zoom > 18f) 0.00006 else LATLNG_LNG
        aMap.animateCamera(
            CameraUpdateFactory.newLatLng(
                LatLng(
                    latLng.latitude - rLat,
                    latLng.longitude + rLng
                )
            )
        )
    }

    /**
     * 设置地图中心LatLng以及缩放级别 默认17
     */
    fun showMapLatLngZoom(aMap: AMap, latLng: LatLng, zoom: Float = 17f) {
        aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom))
    }

    /**
     * 根据点计算可视范围内的地图级别  边框距离默认200
     */
    fun showMapBounds(aMap: AMap, latLngBounds: LatLngBounds?, padding: Int = 200) {
        aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, padding))
    }

    /**
     *设置弹窗显示位置+偏移默认 LATLNG_LAT  LATLNG_LNG
     */
    fun showLatLngZoom(
        aMap: AMap,
        latLng: LatLng,
        lat: Double = LATLNG_LAT,
        lng: Double = LATLNG_LNG
    ) {
        showMapLatLngZoom(
            aMap,
            LatLng(latLng.latitude - lat, latLng.longitude + lng),
            17f
        )
    }

    /**
     *设置弹窗显示位置+偏移默认 LATLNG_LAT  LATLNG_LNG
     */
    fun moveLatLng(
        aMap: AMap,
        latLng: LatLng,
        lat: Double = LATLNG_LAT,
        lng: Double = LATLNG_LNG
    ) {
        showMapLatLng(
            aMap,
            LatLng(latLng.latitude - lat, latLng.longitude + lng),

            )
    }


    /**
     *设置弹窗显示位置+偏移默认 LATLNG_LAT  LATLNG_LNG
     */
    fun showMapPopupLatLng(
        aMap: AMap,
        latLng: LatLng,
        lat: Double = LATLNG_LAT,
        lng: Double = LATLNG_LNG
    ) {
        showMapLatLngZoom(aMap, LatLng(latLng.latitude + lat, latLng.longitude + lng))
    }

    fun initMapSetting(mAMap: AMap, isZoomBtn: Boolean = false) {
        mAMap.uiSettings.run {
//            //关闭一切手势操作
//            setAllGesturesEnabled(false)

            //缩放按钮
            isZoomControlsEnabled = isZoomBtn
            //旋转功能
            isRotateGesturesEnabled = false
            //双指倾斜手势
            isTiltGesturesEnabled = false
            //定位按钮
            isMyLocationButtonEnabled = false
            //开启比例尺
            isScaleControlsEnabled = true
        }
    }

    /**
     * 设置定位相关信息 默认定位1次 不展示定位蓝点
     */
    fun setLocationStyle(aMap: AMap, showLocation: Boolean = false) {
        MyLocationStyle().apply {
            myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW)
            showMyLocation(showLocation)
            aMap.isMyLocationEnabled = true
        }.also { aMap.myLocationStyle = it }

    }

    /**
     *   跳转到拨号界面，同时传递电话号码
     */
    fun callPhone(context: Context, phone: String) {
        val dialIntent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phone"))
        context.startActivity(dialIntent)
    }


    /**
     * 导航
     *
     * @param context
     * @param endName
     * @param endPoint
     */
    fun navi(
        endName: String,
        endPoint: LatLng
    ) {
        val params = AmapNaviParams(
            null, null, Poi(endName, endPoint, ""), AmapNaviType.DRIVER, AmapPageType.ROUTE
        )
        params.setUseInnerVoice(true)
        params.setNeedDestroyDriveManagerInstanceWhenNaviExit(true)
        AmapNaviPage.getInstance()
            .showRouteActivity(AppContext.getAppContext(), params, mNaviInfoCallback)
    }

    init {
        NaviUtil.setNaviListener(object : INaviListener {
            override fun toNavi(plate: String, eLat: Double, eLng: Double) {
                navi(plate, LatLng(eLat, eLng))
            }
        })
    }
}
